package com.mydomain.debtbook;

import com.mydomain.debtbook.ViewDebtActivity.debtNameListAdapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

public class ProfileActivity extends Activity {

	private DebtAdapter debtAdapter;
	private Cursor totalDebtCursor;
	private Cursor youOweDebtCursor;
	private Cursor theyOweDebtCursor;
	private TextView tvTotalDebt;
	private TextView tvYouOweDebt;
	private TextView tvTheyOwelDebt;
	private TextView nameLabel;

	private String mainName;
	private String highlightName;

	private ListView debtNamesList;
	private SimpleCursorAdapter cursorAdapter;
	private Cursor debtNameCursor;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.profile);
		debtNamesList = (ListView) findViewById(R.id.p_listContacts);
		debtAdapter = ((debtBookApplication) this.getApplication())
				.getDatabaseAdapter();
		totalDebtCursor = debtAdapter.getDebtTotalForPersonCursor(mainName,
				DebtAdapter.TOTAL_DEBT);
		youOweDebtCursor = debtAdapter.getDebtTotalForPersonCursor(mainName,
				DebtAdapter.BORROWER_DEBT);
		theyOweDebtCursor = debtAdapter.getDebtTotalForPersonCursor(mainName,
				DebtAdapter.LENDER_DEBT);
		tvTotalDebt = (TextView) findViewById(R.id.p_totalDebtValue);
		tvYouOweDebt = (TextView) findViewById(R.id.p_youOwelDebtValue);
		tvTheyOwelDebt = (TextView) findViewById(R.id.p_theyOweDebtValue);
		nameLabel = (TextView) findViewById(R.id.p_name);
		showDebtNames();
	}

	private void showDebtNames() {
		debtNameCursor = debtAdapter.getAllItemCursor(DebtAdapter.DB_TB_NAMES);
		if (debtNameCursor != null) {
			startManagingCursor(debtNameCursor);
		}

		cursorAdapter = new debtNameListAdapter(this,
				R.layout.contacts_row, debtNameCursor,
				new String[] { DebtAdapter._ITEM },
				new int[] {R.id.cr_name });

		debtNamesList.setAdapter(cursorAdapter);

		debtNamesList.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				mainName = debtNameCursor.getString(debtNameCursor
						.getColumnIndexOrThrow(DebtAdapter._ITEM));

				highlightName = mainName;
				cursorAdapter.notifyDataSetChanged();
				refreshView();
			}
		});
	}

	public class debtNameListAdapter extends SimpleCursorAdapter {

		private LayoutInflater mInflater;
		private Context context;
		private TextView nameTv;
		private String[] myFrom;

		public debtNameListAdapter(Context context, int layout, Cursor cursor,
				String[] from, int[] to) {

			super(context, layout, cursor, from, to);
			mInflater = LayoutInflater.from(context);
			this.context = context;
			myFrom = from;
		}

		@Override
		public void bindView(View view, Context context, Cursor c) {
			int nameCol = c.getColumnIndexOrThrow(myFrom[0]);

			String name = c.getString(nameCol);

			TextView nameTv = (TextView) view.findViewById(R.id.cr_name);

			nameTv.setText(name);

			if (name.equals(highlightName)) {
				nameTv.setTextColor(Color.parseColor("#778899"));
			} else {
				nameTv.setTextColor(Color.WHITE);
			}

		}
	}

	private void refreshView() {
		nameLabel.setText(mainName);
		totalDebtCursor = debtAdapter.getDebtTotalForPersonCursor(
				mainName, DebtAdapter.TOTAL_DEBT);
		youOweDebtCursor = debtAdapter.getDebtTotalForPersonCursor(
				mainName, DebtAdapter.BORROWER_DEBT);
		theyOweDebtCursor = debtAdapter.getDebtTotalForPersonCursor(
				mainName, DebtAdapter.LENDER_DEBT);
		totalDebtCursor.moveToFirst();
		youOweDebtCursor.moveToFirst();
		theyOweDebtCursor.moveToFirst();
		String totalDebtString = totalDebtCursor.getString(0);
		String youOweDebtString = youOweDebtCursor.getString(0);
		String theyOweDebtString = theyOweDebtCursor.getString(0);

		if (totalDebtString != null) {

			tvTotalDebt.setText("$"
					+ String.format("%.2f",
							Double.parseDouble(totalDebtString)));
		} else {
			tvTotalDebt.setText("$0.00");
		}

		if (youOweDebtString != null) {
			tvYouOweDebt.setText("$"
					+ String.format("%.2f",
							Double.parseDouble(youOweDebtString)));
		} else {
			tvYouOweDebt.setText("$0.00");
		}

		if (theyOweDebtString != null) {
			tvTheyOwelDebt.setText(theyOweDebtString);
			tvTheyOwelDebt.setText("$"
					+ String.format("%.2f",
							Double.parseDouble(theyOweDebtString)));
		} else {
			tvTheyOwelDebt.setText("$0.00");
		}

	}
	

	@Override
	protected void onActivityResult(int requestCode, int resultCode,
			Intent intent) {
		super.onActivityResult(requestCode, resultCode, intent);
		switch (requestCode) {
		case 0:

			if (resultCode == RESULT_OK && intent.hasExtra("ItemId")) {

				mainName = intent.getStringExtra("ItemId");
				break;
			}
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.menu_viewdebt, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.mvd_new:
			startActivity(new Intent(this, NewDebtActivity.class));
			return true;
		case R.id.mvd_switch:

			return true;
		case R.id.mvd_setting:
			startActivityForResult(new Intent(this, ListNameActivity.class), 0);
			return true;
		}
		return false;
	}

	@Override
	public void onResume() {
		super.onResume();

	}

}
